﻿#include "Player.h"
#include "GameUtil.h"

using namespace cocos2d;

#define SHIELD_W 3.0f
#define SHIELD_H 3.8f

Player::~Player(void)
{
}

bool Player::init(){  
    if (!Creature::init()) {  
        return false;  
    }  

	// init
	d_creatureSprite = CCSprite::create("baby.png");
	
	// init Shield
	CCSize spriteSize = d_creatureSprite->getContentSize();
	d_pShield = CCSprite::create("light.png");
	ccBlendFunc blendFunc = {GL_SRC_ALPHA, GL_ONE}; 
	d_pShield->setBlendFunc(blendFunc);
	d_pShield->setScale(.0f);
	this->addChild(d_pShield, 8);
	d_pShield->setPosition(ccp(spriteSize.width/2 + 10, spriteSize.height/2));

	d_state = STATE_DEFENCE;
	d_objType = OBJ_TYPE_PLAYER;
	d_hasHpBar = false;

	d_shootInterval = 20;
	d_bulletSpeed = 600;
	d_bulletDamage = GameUtil::instance()->getSetting()->getPlayerDamage();
	d_bulletType = BULLET_TYPE_5;
	
	d_moveSpeed = d_fieldSize.height/2;

	this->setPosition(ccp(200.0f, d_fieldSize.height/2));
	d_moveMax = d_fieldSize.height - d_creatureSprite->getContentSize().height / 2;
	d_moveMin = d_creatureSprite->getContentSize().height / 2;

    return true;  
} 

void Player::setTouchStart(float y)
{
	if(d_state == STATE_DEFENCE) {
		d_state = STATE_ATTACK;
		GameUtil::instance()->getShield()->setActive(false);
		this->setPShieldActive(true);
		this->startShoot(false);
		// tooth shoot
		GameUtil::instance()->getToothManager()->startShoot();
	}
	if(y > d_moveMax) {
		y = d_moveMax;
	}
	if(y < d_moveMin) {
		y = d_moveMin;
	}
	d_moveDst = y;
}


void Player::update(float dt)
{
	if(d_state == STATE_ATTACK) {
		int y = this->getPositionY();
		if(d_moveDst > y) {
			y += dt*d_moveSpeed;
			if(d_moveDst < y) {
				y = d_moveDst;
			}
		} else {
			y -= dt*d_moveSpeed;
			if(d_moveDst > y) {
				y = d_moveDst;
			}
		}
		this->setPositionY(y);
	}

}

void Player::setTouchEnd()
{
	d_state = STATE_DEFENCE;
	GameUtil::instance()->getShield()->setActive(true);
	this->setPShieldActive(false);
	this->stopShoot();
	// tooth shoot
	GameUtil::instance()->getToothManager()->stopShoot();
}

void Player::shoot(float dt)
{
	d_bulletManager->shoot(this, 0);
}

CCRect Player::getHitRect()
{
	return GameUtil::getRect(1.1f, d_creatureSprite);
}

bool Player::hit(float damage)
{
 //   d_creatureSprite->setColor(ccc3(255,0,0));
	//if(d_actionTintTag != kCCActionTagInvalid) {
	//	d_creatureSprite->stopActionByTag(d_actionTintTag);
	//}
	//CCAction* action = CCTintTo::create(0.5f, 255, 255, 255);
	//d_actionTintTag = action->getTag();
	//d_creatureSprite->runAction(action);

	//return false;

	// hit animation
    d_pShield->setColor(ccc3(0, 128, 255));
	if(d_actionTintTag != kCCActionTagInvalid) {
		d_pShield->stopActionByTag(d_actionTintTag);
	}
	CCAction* action = CCTintTo::create(0.5f, 255, 255, 255);
	d_actionTintTag = action->getTag();
	d_pShield->runAction(action);

	return false;
}

void Player::setPShieldActive(bool active)
{
	if(active) {
		d_pShield->setColor(ccc3(255, 255, 255));
		d_pShield->stopAllActions();
		d_pShield->runAction(CCScaleTo::create(0.2f, SHIELD_W, SHIELD_H));
	} else {
		d_pShield->stopAllActions();
		d_pShield->runAction(CCScaleTo::create(0.2f, .0f, .0f));
	} 

}

void Player::onUpdrade()
{
	d_bulletDamage = GameUtil::instance()->getSetting()->getPlayerDamage();
}